//
//  IndexProgressView.swift
//  gamlify
//
//  Created by Admin on 2024/12/8.
//

import UIKit

import UIKit
import SnapKit

class IndexProgressView: UIView {
    // MARK: - Properties
    private let progressLines: [UIImageView] = (0..<3).map { _ in
        let imageView = UIImageView()
        imageView.image = UIImage(named: "progress_line_gray")
        return imageView
    }
    
    private let progressPoints: [UIImageView] = (0..<4).map { _ in
        let view = UIImageView()
        view.image = UIImage(named: "dot_normal")
        return view
    }
    
    private let progressLabels: [UILabel] = (0..<4).map { _ in
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 10)
        label.textColor = UIColor.hex(0x899AB2)
        label.textAlignment = .center
        label.text = "+2"
        return label
    }
    
    var progress: Int = 0 {
        didSet {
            if progress < 3 {
                updateProgress()
            }
        }
    }
    
    // MARK: - Initialization
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 4个点，等分布局
        let pointWidth = 6
        let pointMargin = (Int(frame.width) - pointWidth * 4 - 8) / 3
        
        for (index, point) in progressPoints.enumerated() {
            point.snp.makeConstraints { make in
                make.width.height.equalTo(pointWidth)
                make.top.equalToSuperview().offset(4)
                make.left.equalToSuperview().offset(4 + (pointWidth + pointMargin) * index)
            }
        }
        
        for (index, line) in progressLines.enumerated() {
            line.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(4)
                make.height.equalTo(3.5)
                make.width.equalTo(pointMargin)
                make.left.equalTo(progressPoints[index].snp.right)
            }
        }
        
        for (index, label) in progressLabels.enumerated() {
            label.snp.makeConstraints { make in
                make.top.equalTo(progressPoints[index].snp.bottom).offset(2)
                make.centerX.equalTo(progressPoints[index])
                make.height.equalTo(14)
            }
        }
    }
    // MARK: - Setup
    private func setupViews() {
        progressLines.forEach { addSubview($0) }
        progressPoints.forEach { addSubview($0) }
        progressLabels.forEach { addSubview($0) }
    }
    
    // MARK: - Update Progress
    private func updateProgress() {
        for (index, point) in progressPoints.enumerated() {
            if index <= progress {
                point.image = UIImage(named: "dot_selected")
            } else {
                point.image = UIImage(named: "dot_normal")
            }
        }
        
        progressPoints.last?.image = UIImage(named: "dot_complete")
        
        for (index, label) in progressLabels.enumerated() {
            if index <= progress {
                label.textColor = UIColor.hex(0x899AB2)
            } else {
                label.textColor = UIColor.hex(0xE9EFF7)
            }
        }
        
        for (index, line) in progressLines.enumerated() {
            if index < progress {
                line.image = UIImage(named: "progress_line_color")
            } else if index == progress {
                line.image = UIImage(named: "progress_line_gradation")
            } else {
                line.image = UIImage(named: "progress_line_gray")
            }
        }
    }
}

